package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameDraw.*;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.draw.TigerDraw;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.ThinkingDataManager;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;

import java.util.ArrayList;
import java.util.List;

public class DrawHandler {
    //CMD_GAME_DRAW_TIGER_MAIN_INFO = 71701; // 玩家的老虎机抽奖记录
    public static Object CMD_GAME_DRAW_TIGER_MAIN_INFO(RequestParam clientParam) throws Exception {
        CSGameDrawTigerMainInfoRequest request = CSGameDrawTigerMainInfoRequest.parseFrom(clientParam.getData());
        CSGameDrawTigerMainInfoResponse.Builder response = CSGameDrawTigerMainInfoResponse.newBuilder();
        long userId = clientParam.getUserId();
        PBUserData.Builder pbUserData = response.getUserBuilder();

        UserTigerDraw userTigerDraw = DrawUserData.getUserTigerDraw(userId);
        if (userTigerDraw != null) {
            ProtoDataUtils.updatePBUserData(pbUserData, userTigerDraw, true);
        }

        return response;
    }

    // CMD_GAME_DRAW_TIGER_DRAW = 71702; // 抽奖
    public static Object CMD_GAME_DRAW_TIGER_DRAW(RequestParam clientParam) throws Exception {
        CSGameDrawTigerDrawRequest request = CSGameDrawTigerDrawRequest.parseFrom(clientParam.getData());
        CSGameDrawTigerDrawResponse.Builder response = CSGameDrawTigerDrawResponse.newBuilder();
        long userId = clientParam.getUserId();
        boolean isTen = request.getIsTen();

        int itemId = ItemId.TIGER_DRAW_ITEM;// 游戏币,用于老虎机抽奖
        UserInfo userInfo = GameUser.getUserInfo(userId);
        long subNum = isTen ? AllParam.TIGER_DRAW_ITEM_PRICE[1] : AllParam.TIGER_DRAW_ITEM_PRICE[0];// 需要扣除的数量
        int num = isTen ? 10 : 1;// 实际的抽卡次数
        if (BagManager.getUserItemNum(userInfo, itemId) < subNum) {// 游戏币不足
            itemId = ItemId.DIAMOND;
            subNum = isTen ? AllParam.TIGER_DRAW_DIAMOND[1] : AllParam.TIGER_DRAW_DIAMOND[0];// 需要扣除的数量
            if (BagManager.getUserItemNum(userInfo, itemId) < subNum) {
                return ErrorWord.ITEM_LACK;// 钻石也不足
            }
        }

        // 执行逻辑
        PBUserData.Builder pbUserData = response.getUserBuilder();
        RewardManager.subReward(userInfo, itemId, subNum, pbUserData, LogType.TIGER_DRAW);// 扣除道具

        UserTigerDraw userTigerDraw = DrawUserData.getUserTigerDraw(userId);
        boolean isFistTen = false;// 玩家第一次十连必定获得
        List<RewardItem> totalRewardItem = new ArrayList<>();// 总的的奖励, 发送到数数用
        if (userTigerDraw == null) {
            userTigerDraw = new UserTigerDraw();
            userTigerDraw.putId(userId);
            userTigerDraw.putNum(num);
            userTigerDraw.putTotalNum(num);
            userTigerDraw.doCreate();
            ProtoDataUtils.updatePBUserData(pbUserData, userTigerDraw, true);
            if (isTen) {
                isFistTen = true;
            }
        } else {
            int nn = userTigerDraw.getNum() + num;
            if (nn >= AllParam.TIGER_DRAW_MUST_REWARD_LIMIT) {// 满300次额外获得招募券
                RewardManager.addReward(userInfo, AllParam.TIGER_DRAW_MUST_REWARD[0], AllParam.TIGER_DRAW_MUST_REWARD[1], pbUserData, LogType.TIGER_DRAW);//
                DrawManager.createTigerDrawRecord(userInfo, AllParam.TIGER_DRAW_MUST_REWARD[0], AllParam.TIGER_DRAW_MUST_REWARD[1]);// // 生成老虎机的抽奖获得记录，只有获得招募券时才记录
                totalRewardItem.add(new RewardItem(AllParam.TIGER_DRAW_MUST_REWARD[0], AllParam.TIGER_DRAW_MUST_REWARD[1]));
                nn = nn - AllParam.TIGER_DRAW_MUST_REWARD_LIMIT;
            }

            if (isTen && userTigerDraw.getIsFirstTen() == 0) {
                isFistTen = true;
                userTigerDraw.putIsFirstTen(1);
            }

            userTigerDraw.putNum(nn);
            userTigerDraw.addTotalNum(num);
            userTigerDraw.update();
            ProtoDataUtils.updatePBUserData(pbUserData, userTigerDraw);
        }

        // 随机奖励
        List<TigerDraw> list = DrawManager.getTigerDrawList();
        int[] rates = new int[list.size()];
        for (int i=0; i< list.size(); i++)  {
            rates[i] = list.get(i).getRate();
        }
        TigerDraw tigerDraw = null;
        for (int i=0;i<num;i++) {
            tigerDraw = list.get(Tool.getWeightIndex(rates));// 抽到的奖励
            List<RewardItem> reward = CommonUtils.takeReawrdItemFromStr(tigerDraw.getReward());// 本抽获得的奖励
            if (isFistTen && CommonUtils.checkContainsItemId(reward, AllParam.TIGER_DRAW_FIRST_TEN_REWARD[0])) {
                isFistTen = false;// 运气好,已经抽到了玩家第一次十连必定获得
            }
            if (i == num - 1 && isFistTen) {// 未获得,则第10抽,给必得的奖励
                RewardManager.addReward(userInfo, AllParam.TIGER_DRAW_FIRST_TEN_REWARD[0], AllParam.TIGER_DRAW_FIRST_TEN_REWARD[1], pbUserData, LogType.TIGER_DRAW);//
                DrawManager.createTigerDrawRecord(userInfo, AllParam.TIGER_DRAW_FIRST_TEN_REWARD[0], AllParam.TIGER_DRAW_FIRST_TEN_REWARD[1]);// // 生成老虎机的抽奖获得记录，只有获得招募券时才记录
                totalRewardItem.add(new RewardItem(AllParam.TIGER_DRAW_FIRST_TEN_REWARD[0], AllParam.TIGER_DRAW_FIRST_TEN_REWARD[1]));
            } else {
                RewardManager.addReward(userInfo, reward, pbUserData, LogType.TIGER_DRAW);//
                for (RewardItem rewardItem : reward) {
                    totalRewardItem.add(rewardItem);
                    DrawManager.createTigerDrawRecord(userInfo, rewardItem.getItemId(), rewardItem.getItemNum());// // 生成老虎机的抽奖获得记录，只有获得招募券时才记录
                }
            }
        }

        // 上报数数
        ThinkingDataManager.pushUserDrawEvent(userInfo, isTen, itemId, totalRewardItem);


        return response;
    }

    // CMD_GAME_DRAW_TIGER_DRAW_RECORD = 71703; // 抽奖获得招募券的记录[3]每次打开请求增量下发
    public static Object CMD_GAME_DRAW_TIGER_DRAW_RECORD(RequestParam clientParam) throws Exception {
        CSGameDrawTigerDrawRecordRequest request = CSGameDrawTigerDrawRecordRequest.parseFrom(clientParam.getData());
        CSGameDrawTigerDrawRecordResponse.Builder response = CSGameDrawTigerDrawRecordResponse.newBuilder();
        long userId = clientParam.getUserId();
        long dbid = request.getDbid();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        List<TigerDrawRecord> list = DrawManager.getTigerDrawRecordList(userInfo.getServerId());
        for (TigerDrawRecord tigerDrawRecord : list) {
            if (tigerDrawRecord.getId() > dbid) {
                response.putRecordList(tigerDrawRecord.getId(), tigerDrawRecord.takeInitBuilder().build());
            }
        }

        return response;
    }
}
